import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: "/choiceness",
    component: HomeView,
    meta: { isok: false },
    children: [
      {
        path: '/choiceness',
        name: 'choiceness',
        component: () => import('../views/choiceness/index.vue'),
        meta: { isok: false },

      },
      {
        path: '/classify',
        name: 'classify',
        component: () => import('../views/classify/index.vue'),
        meta: { isok: false },

      },
      {
        path: '/trolley',
        name: 'trolley',
        component: () => import('../views/trolley/index.vue'),
        meta: { isok: true },
      },
      {
        path: '/mains',
        name: 'mains',
        component: () => import('../views/mains/index.vue'),
        meta: { isok: true },
      }
    ]
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/details/details.vue'),
    meta: { isok: false },
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import('../views/shop/shop.vue'),
    meta: { isok: false },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/login.vue'),
    meta: { isok: false },
  },
  {
    path: '/order',
    name: 'oeder',
    component: () => import('../views/order/order.vue'),
    meta: { isok: false },
  },
  {
    path: '/setorder',
    name: 'setoeder',
    component: () => import('../views/order/setorder.vue'),
    meta: { isok: false },
  },
  {
    path: '/orderadd',
    name: 'orderadd',
    component: () => import('../views/order/orderadd.vue'),
    meta: { isok: false },
  },
  {
    path: '/orderpay',
    name: 'orderpay',
    component: () => import('../views/order/orderpay.vue'),
    meta: { isok: false },
  },
  {
    path: '/mainset',
    name: 'mainset',
    component: () => import('../views/mains/mainset.vue'),
    meta: { isok: false },
  },
  {
    path: '/take',
    name: 'take',
    component: () => import('../views/mains/take.vue'),
    meta: { isok: false },
  },
  {
    path: '/nikename',
    name: 'nikename',
    component: () => import('../views/mains/tacknikename.vue'),
    meta: { isok: false },
  },
]

const router = new VueRouter({
  routes
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log(from.path)
  localStorage.setItem("active", from.path)
  // 判断是否登录
  var token = localStorage.getItem("token")
  if (token) {
    // 登录后不能再去登录页
    if (to.path === '/login') {
      next(from.path)
    } else {
      next()
    }
  } else {
    if (!to.meta.isok) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
